package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.mapper.log.PushDetailLogMapper;
import com.tulele.vtool.bus.mapper.log.PushHeadLogMapper;
import com.tulele.vtool.bus.model.ArticleInfo;
import com.tulele.vtool.bus.model.UserBrandInfo;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.log.PushDetailLog;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.IArticleService;
import com.tulele.vtool.bus.service.IUserBrandService;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.SysGzhService;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SysConstant;
import com.tulele.vtool.util.ToolUtil;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class MsgAsync20210327Task {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private IUserBrandService userBrandService;

    @Autowired
    private IArticleService articleService;


    @Async("taskExecutor")
    public Integer execute20210327(String redisKey, UserInfo userInfo, String profile) throws Exception {

        Integer ret = 2;

        Object pushUserJsonObject = redisCacheUtil.getCacheObject(redisKey);
        WxPushUserInfo wxPushUserInfo = null;
        if (pushUserJsonObject != null) {

            String pushUserJson = (String) pushUserJsonObject;
            log.info("[pushUserJson] = " + pushUserJson);

            wxPushUserInfo = JSONObject.parseObject(pushUserJson, WxPushUserInfo.class);
        } else {
            wxPushUserInfo = new WxPushUserInfo();
            wxPushUserInfo.setOpenId(userInfo.getOpenId());
            wxPushUserInfo.setUserId(userInfo.getUserId());
        }

        if (wxPushUserInfo != null) {
            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);

            WeixinServiceDL weixinService = WeixinServiceMap.getWeixinService(profile);
            if (weixinService == null) {
                weixinService = objectFactory.getObject();
                weixinService.reSetConfigStorage(profile);
                WeixinServiceMap.putWeixinService(profile, weixinService);
            }

            WxMpKefuMessage.WxArticle wxArticle = null;

            List<UserBrandInfo> userBrandInfos = userBrandService.getUserSubBrandInfo(wxPushUserInfo.getUserId());//获取用户的 品牌
            boolean isBaoHan = false;
            for (UserBrandInfo userBrandInfo : userBrandInfos) {
                if (userBrandInfo.getBrandId().equals(37369)) {//博蓝共享
                    isBaoHan = true;
                }
            }

            if (isBaoHan) {

                String tishi = "亲\uD83D\uDCDD最新热文已带上您 的名片\n\n" +
                        "直接打开分享即可\n" +
                        "\uD83D\uDC47\t\t\uD83D\uDC47\t\t\uD83D\uDC47";
                if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                    tishi = ToolUtil.handleMsgmenuidByIOS(tishi);
                }
                WxMpKefuMessage tiWKM = WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(tishi).build();
                List<ArticleInfo> articleInfos = null;
                weixinService.getKefuService().sendKefuMessage(tiWKM);
                articleInfos = articleService.getArticleInfoByBrandForCreateTime(37369, 1);
                if (articleInfos != null && articleInfos.size() > 0) {
                    wxArticle = new WxMpKefuMessage.WxArticle();
                    wxArticle.setTitle(articleInfos.get(0).getArticleTitle());
                    wxArticle.setDescription(articleInfos.get(0).getArticleDesc());
                    wxArticle.setPicUrl(articleInfos.get(0).getArticleImgUrl()+"?"+SysConstant.OSS_PAR_80_80);
                    wxArticle.setUrl(sysGzh.getFrontDomain() + "pages/article/detail.html?t=2&userId=" + wxPushUserInfo.getUserId() + "&articleId=" + articleInfos.get(0).getId());
                }

                WxMpKefuMessage message = WxMpKefuMessage.NEWS().addArticle(wxArticle).toUser(wxPushUserInfo.getOpenId()).build();

                weixinService.getKefuService().sendKefuMessage(message);
            }

        }
        return ret;
    }
}
